package com.dslcode.cloud.provider.member.controller;

import com.dslcode.cloud.common.api.member.MemberAPI;
import com.dslcode.cloud.common.model.member.Member;
import com.dslcode.cloud.common.utils.AjaxResponse;
import com.dslcode.cloud.provider.member.service.MemberService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by dongsilin on 2017/9/21.
 */
@Slf4j
@RestController
public class MemberController implements MemberAPI {

    @Autowired
    private MemberService memberService;

    @Override
    public AjaxResponse<PageInfo<Member>> list(@RequestParam(name = "pageNum", defaultValue = "1") int pageNum, @RequestParam(name = "pageSize", defaultValue = "10") int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Member> members = memberService.findAll();
        log.info("members={}", members);
        return AjaxResponse.buildSuccessData(new PageInfo(members));
    }

    @Override
    public AjaxResponse<Member> detail(@PathVariable(name = "id") Long id) {
        return AjaxResponse.buildSuccessData(memberService.findById(id));
    }

   /**
     * 增加积分
     * @param id
     * @return
     */
    @Override
    public AjaxResponse addPoints(@PathVariable(name = "id") Long id, int points){
        return AjaxResponse.buildSuccessData(memberService.addPoints(id, points));
    }

}
